#include "regester.h"
#include "ui_regester.h"
#include "login.h"
#include <QKeySequence>

regester::regester(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::regester)
{
    ui->setupUi(this);
    QPixmap image(":/image/logo.png");
    ui->imageLabel->setPixmap(image);
    ui->imageLabel->setScaledContents(true);
    QPixmap image_background(":/image/background.jpeg"); //设置登录界面的背景，使其自适应窗口大小
    ui->label_background->setPixmap(image_background);
    ui->label_background->setScaledContents(true);
    ui->pushButton->setShortcut(QKeySequence("Return"));//回车快捷键
}

regester::~regester()
{
    delete ui;
}

void regester::on_changepushButton_clicked()
{
    login *loginWindow = new login;
        loginWindow->show();
        QWidget::close();
}

void regester::on_pushButton_clicked()
{
    QSqlQuery query(db);
    int id = ui->idlineEdit->text().toInt();
    QString name = ui->namelineEdit->text();
    QString pw = ui->pwlineEdit->text();
    QString sqlinsert = QString("insert into regester values(%1,'%2','%3',NULL); ").arg(id).arg(pw).arg(name);//将文本编辑栏中的数据写入sql语句
    if(id == 0)//判断id，密码和名字是否符合标准
        QMessageBox::warning(nullptr,tr("警告"),tr("id不能为空且只能是数字"),QMessageBox::Ok);
    else if(pw == "")
        QMessageBox::warning(nullptr,tr("警告"),tr("密码不能为空"),QMessageBox::Ok);
    else if(name == "")
        QMessageBox::warning(nullptr,tr("警告"),tr("名字不能为空"),QMessageBox::Ok);
    else if(!query.exec(sqlinsert))//执行sql语句
    {
        QSound *soundEffect = new QSound( ":/sound/error.wav");
        soundEffect->play();
        QMessageBox::warning(nullptr,tr("警告"),tr("注册失败"),QMessageBox::Ok);

    }
    else
    {
        QSound *soundEffect = new QSound( ":/sound/success.wav");
        soundEffect->play();
        QMessageBox::information(nullptr,tr("信息"),tr("注册成功"),QMessageBox::Ok);
        login* loginwindow = new login;
        loginwindow->show();
        QWidget::close();
    }
}
